﻿using System;
using System.Collections.Generic;

namespace Inovout.Cloud.DataStore.Utility
{
    /// <summary>
    /// 动态构建表结构的类
    /// </summary>
    public class TableModelHelper
    {
        /// <summary>
        /// 根据表的结构生成对应的类
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public static Type GetType(string tableName)
        {
            Type type = null;
            if (String.IsNullOrEmpty(tableName))
            {
                type = ClassHelper.BuildType();
            }
            else
            {
                type = ClassHelper.BuildType(tableName);
            }

            //先定义属性
            List<ClassHelper.CustPropertyInfo> lcpi = new List<ClassHelper.CustPropertyInfo>();
            ClassHelper.CustPropertyInfo cpi;

            //TODO：从数据库中取得表的定义
            cpi = new ClassHelper.CustPropertyInfo("System.String", "Name");
            lcpi.Add(cpi);

            cpi = new ClassHelper.CustPropertyInfo("System.String", "Type");
            lcpi.Add(cpi);

            cpi = new ClassHelper.CustPropertyInfo("System.Int32", "Price");
            lcpi.Add(cpi);

            //-------------------------------------------------------------
            //其他自定义属性
            //MongoDb自动生成的主键_id
            cpi = new ClassHelper.CustPropertyInfo("System.Object", "Id");
            lcpi.Add(cpi);
            
            //程序往MongoDb中插入记录时，每条记录会创建一个时间戳
            cpi = new ClassHelper.CustPropertyInfo("System.DateTime", "TimeStamp");
            lcpi.Add(cpi);            

            //再加入上面定义的属性到我们生成的类type
            type = ClassHelper.AddProperty(type, lcpi);

            return type;
        }
    }
}
